<template>
  <view class="my-login">
    <uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
    <!-- getUserInfo  1：uni.getUserInfo 了解 2：通过 button open-type:'getUserInfo' @getuserinfo="获取用户信息"  -->
    <!-- <button class="btn" type="warn" open-type="getUserInfo" @getuserinfo="getuserinfo">一键登录</button> -->
    <!-- getUserProfile   -->
    <button class="btn" type="warn" @click="getuserinfo2">一键登录</button>
    <!-- 最新用户：头像填写能力 -->
    <!-- <button class="btn" type="warn" open-type="chooseAvatar" @chooseavatar="getuserinfo3">一键登录</button> -->

    <view class="text">
      登录后尽享更多权益
    </view>
  </view>
</template>

<script>
  export default {
    name: "my-login",
    data() {
      return {

      };
    },
    methods: {
      getuserinfo(res) {
        console.log('用户信息', res);
      },
      async getuserinfo2() {
        // 获取用户信息
        const res = await uni.getUserProfile({
          desc: '用户信息展示'
        })
        console.log('用户信息2：', res);
        this.$store.commit('user/setUserInfo', res.userInfo)
        // 获取token
        const loginRes = await uni.login()
        const res2 = await uni.$http.post('/users/wxlogin', {
          data: {
            encryptedData: res.encryptedData,
            rawData: res.rawData,
            iv: res.iv,
            signature: res.signature,
            code: loginRes.code
          }
        })
        this.$store.commit('user/setToken', res2.message.token)
        // this.$store.commit('user/setToken', '123')
        // 登录成功
        if (this.$store.state.user.backRoute.url) {
          // uni.switchTab({
          //   url: '/pages/cart/cart'
          // })
          // const xxx = 'switchTab'
          // uni[xxx]({
          //   url: '/pages/cart/cart'
          // })
          // 跳转
          uni[this.$store.state.user.backRoute.type]({
            url: this.$store.state.user.backRoute.url
          })
          // 跳转成功清空相应数据
          this.$store.commit('user/setBackRoute', {})
        }


      },
      // getuserinfo3(res) {
      //   console.log('获取用户头像', res);
      // }
    }
  }
</script>

<style lang="scss" scoped>
  .my-login {
    padding: 160rpx 20rpx;
    text-align: center;

    .btn {
      background-color: #c00000;
      border-radius: 40rpx;
      margin-top: 40rpx;
    }

    .text {
      margin-top: 40rpx;
      color: #afafaf;
      font-size: 12px;
    }
  }
</style>